﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Data;

namespace AccountControl
{
    class DataSaver
    {
        public static void SaveAccountsInXMLFile(string XMLfileName, DataTable table)
        {
            try
            {
                FileStream fileStream = new FileStream(XMLfileName, FileMode.Create);
                XmlWriter writer = XmlWriter.Create(fileStream);
                writer.WriteStartDocument();
                writer.WriteStartElement("Accounts");

                foreach (var record in table.Rows)
                {
                    writer.WriteStartElement("Account");
                    writer.WriteElementString("Дата", (((DataRow)record)["Дата"]).ToString());
                    writer.WriteElementString("Банка", ((DataRow)record)["Банка"].ToString());
                    writer.WriteElementString("Сума", ((DataRow)record)["Сума"].ToString());
                    writer.WriteElementString("Период", ((DataRow)record)["Период"].ToString());
                    writer.WriteElementString("Падеж", ((DataRow)record)["Падеж"].ToString());
                    writer.WriteElementString("Собственик", ((DataRow)record)["Собственик"].ToString());
                    writer.WriteEndElement();
                }

                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Flush();
                writer.Close();
                fileStream.Close();
            }
            catch(Exception e)
            {
                throw e;
            }
        }

        public static void SaveArchiveInXMLFile(string XMLfileName, DataTable table)
        {
            try
            {
                FileStream fileStream = new FileStream(XMLfileName, FileMode.Create);
                XmlWriter writer = XmlWriter.Create(fileStream);
                writer.WriteStartDocument();
                writer.WriteStartElement("Archive");

                foreach (var record in table.Rows)
                {
                    writer.WriteStartElement("Record");
                    writer.WriteElementString("Дата", (((DataRow)record)["Дата"]).ToString());
                    writer.WriteElementString("Банка", ((DataRow)record)["Банка"].ToString());
                    writer.WriteElementString("Сума", ((DataRow)record)["Сума"].ToString());
                    writer.WriteElementString("Период", ((DataRow)record)["Период"].ToString());
                    writer.WriteElementString("Падеж", ((DataRow)record)["Падеж"].ToString());
                    writer.WriteElementString("Собственик", ((DataRow)record)["Собственик"].ToString());
                    writer.WriteElementString("Архивиран", ((DataRow)record)["Архивиран"].ToString());
                    writer.WriteEndElement();
                }

                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Flush();
                writer.Close();
                fileStream.Close();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static void SaveHistoryInXMLFile(string XMLfileName, DataTable table)
        {
            try
            {
                FileStream fileStream = new FileStream(XMLfileName, FileMode.Create);
                XmlWriter writer = XmlWriter.Create(fileStream);
                writer.WriteStartDocument();
                writer.WriteStartElement("History");

                foreach (var record in table.Rows)
                {
                    writer.WriteStartElement("Entry");
                    writer.WriteElementString("Дата_и_час", (((DataRow)record)["Дата и час"]).ToString());
                    writer.WriteElementString("Събитие", (((DataRow)record)["Събитие"]).ToString());
                    writer.WriteElementString("Дата", (((DataRow)record)["Дата"]).ToString());
                    writer.WriteElementString("Банка", ((DataRow)record)["Банка"].ToString());
                    writer.WriteElementString("Сума", ((DataRow)record)["Сума"].ToString());
                    writer.WriteElementString("Период", ((DataRow)record)["Период"].ToString());
                    writer.WriteElementString("Падеж", ((DataRow)record)["Падеж"].ToString());
                    writer.WriteElementString("Собственик", ((DataRow)record)["Собственик"].ToString());
                    writer.WriteEndElement();
                }

                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Flush();
                writer.Close();
                fileStream.Close();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static void SaveBanksDataInXMLFile(string XMLfileName, List<string> banks)
        {
            try
            {
                FileStream fileStream = new FileStream(XMLfileName, FileMode.Create);
                XmlWriter writer = XmlWriter.Create(fileStream);
                writer.WriteStartDocument();
                writer.WriteStartElement("Banks");

                foreach (var bank in banks)
                {
                    writer.WriteStartElement("Bank");
                    writer.WriteElementString("Name", bank);
                    writer.WriteEndElement();
                }

                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Flush();
                writer.Close();
                fileStream.Close();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static void SaveCurrencyDataInXMLFile(string XMLfileName, List<string> currencyData)
        {
            try
            {
                FileStream fileStream = new FileStream(XMLfileName, FileMode.Create);
                XmlWriter writer = XmlWriter.Create(fileStream);
                writer.WriteStartDocument();
                writer.WriteStartElement("CurrencyData");

                foreach (var currency in currencyData)
                {
                    writer.WriteStartElement("Currency");
                    writer.WriteElementString("Name", currency);
                    writer.WriteEndElement();
                }

                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Flush();
                writer.Close();
                fileStream.Close();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static void SaveOwnersDataInXMLFile(string XMLfileName, List<string> owners)
        {
            try
            {
                FileStream fileStream = new FileStream(XMLfileName, FileMode.Create);
                XmlWriter writer = XmlWriter.Create(fileStream);
                writer.WriteStartDocument();
                writer.WriteStartElement("Owners");

                foreach (var owner in owners)
                {
                    writer.WriteStartElement("Owner");
                    writer.WriteElementString("Name", owner);
                    writer.WriteEndElement();
                }

                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Flush();
                writer.Close();
                fileStream.Close();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}
